package com.yupi.usercenter.basic.enums;

import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * 云路供应链科技有限公司 版权所有  Copyright 2019
 *
 * @author linzijing
 * @since Created in 2019/6/19 17:05
 */
public enum CssApiRedisKeyEnum {

    CSSAPI_SYS_NETWORKS_LIST("CSS_API", "SYS", "NETWORKINFO", "LIST", "缓存所有网点"),

    CSSAPI_SYS_NETWORKS_LISTCODE("CSS_API", "SYS", "NETWORKINFO", "LISTCODE", "以code为key缓存"),
    //判断重复工单key
    CSSAPI_SYS_CHECKREPEAT_KEY("CSS_API", "SYS", "CHECKREPEAT", "KEY", "问题件id or运单号+一级类型为key缓存"),

    CSSAPI_SYS_MONITORCONFIG_KEY("CSS_API", "SYS", "MONITORCONFIG", "KEY", "监控工单 流程配置"),

    ;


    /**
     * 系统标识
     */
    private String keyPrefix;
    /**
     * 模块名称
     */
    private String module;
    /**
     * 方法名称
     */
    private String func;
    /**
     * key
     */
    private String key;

    /**
     * 描述
     */
    private String remark;

    CssApiRedisKeyEnum(String keyPrefix, String module, String func, String key, String remark) {
        this.keyPrefix = keyPrefix;
        this.module = module;
        this.func = func;
        this.key = key;
        this.remark = remark;
    }


    /**
     * @param key
     * @return
     */
    public String keyBuilder(String key) {
        return toKey(key);
    }


    /**
     * 组装缓存key
     *
     * @return
     */
    public String keyBuilder() {
        return toKey(this.key);
    }

    public String getKeySplit() {
        return  ":";
    }

    /**
     * @param key
     * @return
     */
    private String toKey(String key) {
        Assert.notNull(keyPrefix, "RedisKeyEnum: keyPrefix can not be null");
        Assert.notNull(module, "RedisKeyEnum: module can not be null");
        Assert.notNull(func, "RedisKeyEnum: func can not be null");
        Assert.notNull(key, "RedisKeyEnum: key can not be null");
        String splitKey = getKeySplit();
        StringBuffer sb = new StringBuffer(keyPrefix);
        sb.append(splitKey).append(module);
        sb.append(splitKey).append(func);
        if (StringUtils.hasLength(key)) {
            sb.append(splitKey).append(key);
        }
        return sb.toString();
    }
}
